/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.ui.base.BaseActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Address;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.data.cache.DataCacheImpl;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.AddressManagerActivity;
import com.simon.mengkou.ui.dialog.DeleteAddressDialog;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/20.
 * desc    : 地址管理列表适配器
 */
public class AddressManagerAdapter extends AbsAdapter<Address> {
    private Address mDefaultAddress;
    private AddressManagerActivity mActivity;

    public AddressManagerAdapter(AddressManagerActivity context) {
        super(context);
        mActivity = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_address_manager_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Address data = getItem(position);
        //地址编辑
        holder.mLlRoot.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                OuerDispatcher.presentEditAddressActivity(mContext, data);
            }
        });

        //默认地址
        if(data.isDefault()) {
            mDefaultAddress = data;
            holder.mTvDefault.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_select_pressed, 0, 0, 0);
        } else {
            holder.mTvDefault.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_select_normal, 0, 0, 0);
        }

        holder.mTvDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mDefaultAddress != data) {
                    AgnettyFuture future = OuerApplication.mOuerFuture.defaultAddress(data.getId(),
                            new OuerFutureListener(mActivity){
                                @Override
                                public void onStart(AgnettyResult result) {
                                    super.onStart(result);
                                    mActivity.setWaitingDialog(true);
                                }

                                @Override
                                public void onComplete(AgnettyResult result) {
                                    super.onComplete(result);
                                    mActivity.setWaitingDialog(false);
                                    if(mDefaultAddress != null) {
                                        mDefaultAddress.setDefault(false);
                                    }

                                    mDefaultAddress = data;
                                    data.setDefault(true);
                                    OuerApplication.mCacheFactory.getCache(DataCacheImpl.AddressDefaultCache.class,
                                            OuerApplication.mUser.getUserId()).save(mDefaultAddress);
                                    notifyDataSetChanged();
                                }

                                @Override
                                public void onException(AgnettyResult result) {
                                    super.onException(result);
                                    mActivity.setWaitingDialog(false);
                                }

                                @Override
                                public void onNetUnavaiable(AgnettyResult result) {
                                    super.onNetUnavaiable(result);
                                    mActivity.setWaitingDialog(false);
                                }
                            });

                    mActivity.attachDestroyFutures(future);
                }
            }
        });

        //删除
        holder.mTvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DeleteAddressDialog(mActivity, data).show();
            }
        });

        //姓名
        holder.mTvName.setText(data.getName());
        //联系方式
        holder.mTvPhone.setText(data.getPhone());
        //详细地址
        holder.mTvAddress.setText(data.getAddress() + data.getDetail());

        return convertView;
    }

    /**
     * @author zhenshui.xia
     */
    class Holder {
        //
        @Bind(R.id.address_id_root)
        LinearLayout mLlRoot;
        //默认地址
        @Bind(R.id.address_id_default)
        TextView mTvDefault;
        //删除
        @Bind(R.id.address_id_delete)
        TextView mTvDelete;
        //姓名
        @Bind(R.id.address_id_name)
        TextView mTvName;
        //手机号码
        @Bind(R.id.address_id_phone)
        TextView mTvPhone;
        //详细地址
        @Bind(R.id.address_id_address)
        TextView mTvAddress;
    }
}
